<?php

namespace App\Http\Controllers\Api;

use App\ForumSort;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ForumSortsController extends BaseController
{
    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $orderBy = $request->order_by ? $request->order_by : 'id';
        $orderType = $request->order_type ? $request->order_type : 'desc';
        $lists = ForumSort::orderBy($orderBy, $orderType);
        if ($request->get('name')) {
            $lists = $lists->where('name', $request->name);
        }
        return $lists->get();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(\App\Http\Requests\ForumSort $request)
    {
        $res = ForumSort::create($request->only(['name']));
        if ($res) {
            return response(['message' => '创建成功']);
        }
        return response(['message' => '创建失败'], 400);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\ForumSort $forumSort
     * @return \Illuminate\Http\Response
     */
    public function show(ForumSort $forumSort)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\ForumSort $forumSort
     * @return \Illuminate\Http\Response
     */
    public function edit(ForumSort $forumSort)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\ForumSort $forumSort
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ForumSort $forumSort)
    {
        $res = $forumSort->update($request->only(['name']));
        if ($res) {
            return response(['message' => '修改成功']);
        }
        return response(['message' => '修改失败'], 400);
    }

    /**
     * @param ForumSort $forumSort
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function destroy(ForumSort $forumSort)
    {
        $forumSort->delete();
        return response(['message' => '删除成功']);
    }
}
